<?php
include_once("./lib/timespan_settings.php");
include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
include_once(dirname(__FILE__) . "/libs/nms_time_system.php");
include_once(dirname(__FILE__) . "/libs/nms_html_script_report.php");
include_once(dirname(__FILE__) . "/model/nms_report.php");
function nms_alarm_report_draw(){
	html_script_report_alarm();
	html_reload_report_alarm();
	nms_alarm_repost_header();
	if (empty($_REQUEST['shost'])) {
		$host_report="";
	}
	else{
		$host_report =  $_REQUEST['shost'];
	}
	if (empty($_REQUEST['sgroup'])) {
		$id_group="";
	}
	else{
		$id_group =  $_REQUEST['sgroup'];
	}
	if (empty($_REQUEST['id_hs'])) {
		$host_id="";
	}
	else{
		$host_id =  $_REQUEST['id_hs'];
	}
	if (empty($_REQUEST['id_gr'])) {
		$group="";
	}
	else{
		$group =  $_REQUEST['id_gr'];
	}
	if (empty($_REQUEST['presets'])) {
		$presets="";
	}
	else{
		$presets =  $_REQUEST['presets'];
	}
	if (empty($_REQUEST['limit'])) {
		$limit="1";
	}
	else{
		$limit =  $_REQUEST['limit'];
	}
	if (empty($_REQUEST['top'])) {
		$top="";
	}
	else{
		$top =  $_REQUEST['top'];
	}
	if(isset($_REQUEST['shost']) or isset($_REQUEST['sgroup'])){
		if($_REQUEST['shost']=="select" && $_REQUEST['sgroup']!="select_group" ){	
		$url= "nms.php?run=report_alarm&shost=".$host_report."&sgroup=".$id_group."&id_hs=".$host_id."&presets=".$presets."&limit=".$limit."&top=".$top;
		}elseif ($_REQUEST['sgroup']=="select_group" && $_REQUEST['shost']!="select"){
		$url= "nms.php?run=report_alarm&shost=".$host_report."&sgroup=".$id_group."&id_gr=".$group."&presets=".$presets."&limit=".$limit."&top=".$top;	
		}elseif ($_REQUEST['sgroup']=="select_group" && $_REQUEST['shost']=="select"){
		$url= "nms.php?run=report_alarm&shost=".$host_report."&sgroup=".$id_group."&id_gr=".$group."&id_hs=".$host_id."&presets=".$presets."&limit=".$limit."&top=".$top;
		}
	}
	if(isset($_REQUEST['shost']) or isset($_REQUEST['sgroup'])){
		if($_REQUEST['shost']=="select" or $_REQUEST['sgroup']=="select_group"){
			nms_alarm_repost_body($url);
		}
	}
	nms_report_fotter();
	?>
		<script type="text/javascript">
			$("#id_host_report").val('<?php echo $host_report;?>');
			$("#id_group_interface").val('<?php echo $id_group;?>');
			$("#id_host").val('<?php echo $host_id;?>');
			$("#id_group").val('<?php echo $group;?>');
			$("#id_presets_report").val('<?php echo $presets;?>');
			$("#get_limit").val('<?php echo $limit;?>');
			$("#id_top").val('<?php echo $top;?>');
			
		</script>
	<?php 
	if(isset($_POST['btnAdd'])){
		if($_REQUEST['shost']=="select"){
			$id = $_REQUEST['id_hs'];
			$name ="interface_report";
			if(check_insert_host($id,$name)==true){
			echo'	
				<script>
					alert("Host existence !!!");
				</script>';
			}
			else {
				insert_host($id,$name);		
			}
			echo '
		  		<script>
		  			reLoad1();
		  		</script>
		  		';
		}
	}
	if(isset($_POST['btnAdd1'])){
		if($_REQUEST['sgroup']=="select_group"){
		$id = $_REQUEST['id_gr'];
		$name ="interface_report";
		if(check_insert_group($id, $name)==true){
			echo'
				<script>
					alert("Group interface existence !!!");
				</script>';
		}else{
		insert_group_interface($id,$name);
		}
		echo '
		  		<script>
		  			reLoad1();
		  		</script>
		  		';
	}
	}
	if(isset($_REQUEST['action'])){
		if($_REQUEST['action']=='delete'){
			$id = $_REQUEST['id'];
			$name ="interface_report";
			delete_one_host_report($id,$name);
			echo '
		  		<script>
		  			reLoad1();
		  		</script>
		  		';
		}
		if($_REQUEST['action']=='delete1'){
			$id = $_REQUEST['id1'];
			$name ="interface_report";
			delete_one_group_report($id,$name);
			echo '
		  		<script>
		  			reLoad1();
		  		</script>
		  		';
		}
	}
}

function nms_alarm_repost_header(){
	?>
	<div id="id_report_1" >
	<div id="id_master_header">
				<h2> Interface Status Report </h2>
	</div>
	<div>&nbsp;</div>
	<div id="id_report_header">
		<div id="id_report_header1">

			<strong>Device:</strong>
				<select  onchange="val()" id="id_host_report">
							<option value=""></option>
		    				<option value="all">All Host</option>
		    				<option value="select">Select Host</option>
		     	</select>
		</div>
		<div id ="id_report_header2">
			
			<strong>Group Interface:</strong>
	  					<select  onchange="val()" id="id_group_interface">
	  						<option value=""></option>
		    				<option value="all_group">All Group Interface</option>
		    				<option value="select_group">Select Group Interface</option>
						</select>
		</div>
		<div class="vide"></div>
	</div>
<?php 
}
function nms_alarm_repost_body($url){
	?>
		<div id="id_report_1" >
			<div id="id_master_header">
				<h2>  </h2>
			</div>
			<div id ="id_report_body">
			<form action="<?php echo $url?>" method ="post"enctype="multipart/form-data" >
				<div id="id_report_body1">
					<?php if($_REQUEST['shost']=="select"){ ?>
					<table>
						<tr>
						<td>
						<strong>Device:</strong>
						</td>
					
						<td>
						<select  onchange="val()" id="id_host">
							<option value=""></option>
		    				<?php
	     					 $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host where description !='Localhost' order by description,hostname");  $result3 = db_fetch_assoc($sql54);
	     					 if (sizeof($hosts) > 0) {
						     foreach($hosts as $set) {
						     echo "<option value='" . $set["id"] . "'";
						     if(isset($_REQUEST["id_hs"])){if($_REQUEST["id_hs"] == $set["id"]){echo "selected";}};
						     echo ">" . htmlspecialchars($set["name"]) . "</option>\n";
						     }
						     }
	   						?>
		     			</select>
		     			<script type="text/javascript">
						$("#id_host").chosen({
							search_contains: true,
							no_results_text:"Oops, nothing found!"});
 						</script>
		     			</td>
		     			<td nowrap style='white-space: nowrap;'>
						&nbsp;<input type="submit" name="btnAdd" id="btnAdd"  value="Add" />
						</td>
						</tr>
					</table>
					<table  width="100%">
									<tr class="cls_master_table_title" >	
										<th width="5%" scope="col">Id</th>
										<th width="30%" scope="col">Host_name</th>
										<th width="40%" scope="col">Description</th>
										<th width="1%" scope="col"></th>
									</tr>
									<?php 
									$data_host = db_fetch_assoc('SELECT host_id , hostname,description from plugin_nms_report_host a ,host b
																where a.host_id =b.id and a.report_group ="interface_report" order by host_id');
									$stt =0;
									foreach ($data_host as $set){
									$stt++;
									?>
									<tr <?php if($stt%2==0) {echo 'class="xanhlot"';}
				 						else {echo 'class="xanhhong"';} ?>> 
						 				
										<td align="center">&nbsp;<?php echo $set['host_id']?></td>
										<td align="center">&nbsp;<?php echo $set['hostname']?></td>
										<td align="center">&nbsp;<?php echo $set['description']?></td>
										<td align="center"><a href="<?php echo $url?>&action=delete&id=<?php echo $set['host_id'];?>"onclick="return confirm('Are you sure you want to delete?')"><?php $stt;echo'Delete';?></a></td>
									</tr>
									<?php 
									}?>
					</table>
					<?php }?>
				</div>
				<div id ="id_report_body2">
					<?php if($_REQUEST['sgroup']=="select_group"){ ?>
					<table>
						<tr>
						<td>
						<strong>Group_interface:</strong>
						</td>
						<td>
						<select  onchange="val()" id="id_group">
		    				<?php
								$hosts = db_fetch_assoc("select interface_group_id,group_name from plugin_nms_groupname order by group_name");
								if (sizeof($hosts) > 0) {
									foreach($hosts as $set) {
										echo "<option value='" . $set["interface_group_id"] . "'"; echo ">" . htmlspecialchars($set["group_name"]) . "</option>\n";
									}
								}
								?>
		     			</select>
		     			</td>
		     			<td nowrap style='white-space: nowrap;'>
						&nbsp;<input  type="submit" name="btnAdd1" id="btnAdd1" type="button" value="Add" />
						</td>
						</tr>
					</table>
					<table width="100%">
									<tr class="cls_master_table_title" >
										<th width="5%" scope="col">Id</th>
										<th width="40%" scope="col">Group_Interface</th>
										<th width="1%" scope="col"></th>
									</tr>
									<?php 
									$data_group = db_fetch_assoc('SELECT a.interface_group_id  as id , group_name from plugin_nms_report_group a ,plugin_nms_groupname b
																where a.interface_group_id =b.interface_group_id and a.report_group ="interface_report" order by a.interface_group_id');
									$stt=0;
									foreach ($data_group as $item){
									$stt++;
									?>
									<tr <?php if($stt%2==0) {echo 'class="xanhlot"';}
				 						else {echo 'class="xanhhong"';} ?>> 
										<td align="center">&nbsp;<?php echo $item['id']?></td>
										<td align="center">&nbsp;<?php echo $item['group_name']?></td>
										<td align="center"><a href="<?php echo $url?>&action=delete1&id1=<?php echo $item['id'];?>"onclick="return confirm('Are you sure you want to delete?')"><?php $stt;echo'Delete';?></a></td>	
									</tr>
									<?php 
									}?>
									
					</table>
					<?php }?>
				</div>
				<div class="vide"></div>
				</form>
			</div>
	<?php 
}
function nms_report_fotter(){
	?>
		<div id="id_master_header">
			<h2>  </h2>
		</div>
		<div id="id_master_center_top">
			<div id="id_master_center_top1">
			<table border="0">
    			<tr>
    				<td>&nbsp;</td>
    				<td> <strong id="">Presets:</strong> </td>
    				<td>
							<select onchange="val()"  id="id_presets_report">
								<option value=""></option>
								<?php
									draw_time_filter();
								?>
								</select>
					</td>
	  				<!-- Button Calendar -->
	  					<?php 
	  					if (empty($_REQUEST['presets'])) {
	  						$_SESSION["current_date"] =date("Y-m-d H:i", strtotime('-1 days'));
							$_SESSION["current_date_2"] =date('Y-m-d H:i');
	  					}
	  					else{
	  						$id = $_REQUEST['presets'];
	  						$_SESSION["current_date"] =nms_time_from($id);
	  						$_SESSION["current_date_2"]= nms_time_to($id);
	  						
	  					}
						?>
						<td nowrap style='white-space: nowrap;'>
							<input type='text' name='date1' id='date1' title='Graph Begin Timestamp' size='15' value='<?php print (isset($_SESSION["current_date"]) ? $_SESSION["current_date"] : "");?>'>
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<input type='image' src='images/iconCalendar.gif' align='middle' alt='Start date selector' title='Start date selector' onclick="return showCalendar('date1');">
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<strong>To:</strong>&nbsp;
						</td>
						<td nowrap style='white-space: nowrap;'>
							<input type='text' name='date2' id='date2' title='Graph End Timestamp' size='15' value='<?php print (isset($_SESSION["current_date_2"]) ? $_SESSION["current_date_2"] : "");?>'>
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<input type='image' src='images/iconCalendar.gif' align='middle' alt='End date selector' title='End date selector' onclick="return showCalendar('date2');">
						</td>
						<td>
							<strong id="">Top changes:</strong></td>
						<td>
							<select  onchange="val()" id="id_top">
								<option value="1">10</option>
								<option value="2">20</option>
								<option value="3">30</option>
								<option value="4">40</option>
								<option value="5">50</option>
								<option value="6">100</option>
							</select>
						</td>
		        		<td>
							<strong id="">Limit number:</strong></td>
							<td>
							<input type ="text" id="get_limit" size="1"/>
						</td>
		  				<td nowrap style='white-space: nowrap;'>
							&nbsp;<input id="btnGo1" type="button"  value="Export" title="Export to a file">
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<input id="btnClear1" type="button" value="Clear"/>
						</td>
						<td>
				  	 		<h4>Refresh time: </h4>
				  	 	</td>
				  	 	<td>
				  	 		<h4><div id ="countdown_time2"></div></h3>
				  	 	</td> 		
			    </tr>
    		</table> 
    		</div>
    	</div>
    	</div>
	<?php 
	html_alarm_script_time();
}
?>